package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;


	@Override
	public IPage<UserAccountDetail> findUserRechargePage(Long userId, Page<UserAccountDetail> userAccountDetailPage) {
		// ACCOUNT_TRADE_TYPE_DEPOSIT="1201";  // 充值
		return userAccountDetailMapper.selectPage(userAccountDetailPage,new LambdaQueryWrapper<UserAccountDetail>()
				.eq(UserAccountDetail::getUserId, userId)
				.eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT));
	}

	@Override
	public IPage<UserAccountDetail> findUserConsumePage(Long userId, Page<UserAccountDetail> userAccountDetailPage) {
		//ACCOUNT_TRADE_TYPE_MINUS="1204";  // 消费
		return userAccountDetailMapper.selectPage(userAccountDetailPage,new LambdaQueryWrapper<UserAccountDetail>()
				.eq(UserAccountDetail::getUserId, userId)
				.eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void checkAndDeduct(AccountDeductVo accountDeductVo) {
		int count = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(),accountDeductVo.getAmount());
		//  判断
		if (count == 0 ){
			log.error("账户余额不足！");
			throw new GuiguException(ResultCodeEnum.ACCOUNT_LESS);
		}
		//  记录明细。将用户的资金流向记录到user_account_detail表
		this.addUserAccountDetail(
				accountDeductVo.getUserId(),
				accountDeductVo.getContent(),
				SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,
				accountDeductVo.getAmount(),
				accountDeductVo.getOrderNo()
		);

	}

	@Override
	public void addUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		userAccountDetailMapper.insert(userAccountDetail);
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		// 查询用户账户余额
		BigDecimal availableAmount = userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId)).getAvailableAmount();
		// 返回数据
		return availableAmount;
	}

	@Override
	public void userRegister(Long userId) {

		//创建用户账户对象
		UserAccount userAccount = new UserAccount();
		//赋值
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal("100.00"));
		userAccount.setAvailableAmount(new BigDecimal("100.00"));
		userAccountMapper.insert(userAccount);


	}
}
